var $ = getApp(), methods = require("../methods.js");
var interval = null;
Page(Object.assign({}, methods, {
	data: {
		tabnum: 1,
		buydialogHidden: true,
		num: 1,
		isfavorite: false,
		btntype: 1,
		ggselected: [],
		ks: '',
		gwcnum: 1,
		nodata: 0
	},
	_onLoad:function(){
		var that = this
		//var id = 3308;
		var proid = that.options.proid
		var joinid = that.options.joinid ? that.options.joinid : '';
		if (that.options && that.options.scene) {
			var scene = that.options.scene
			var scenes = scene.split('_');
			if(scenes[1]){
				proid = parseInt(scenes[1]);
				joinid = parseInt(scenes[2]);
			}
		}
		clearInterval(interval);
		$.get('kanjia/join', { proid:proid,joinid:joinid}, function (res) {
			if (res.status == 0) {
				$.alert(res.msg); return;
			}
			that.setData(res);
			var pagecontent = JSON.parse(res.product.detail);
			var _bannerindex = [];
			for(var i in pagecontent){
				_bannerindex[i] = 0;
			}
			that.setData({pagecontent:pagecontent,_bannerindex:_bannerindex});
			
			interval = setInterval(function(){
				that.setData({nowtime: that.data.nowtime+1})
				that.getdjs();
			},1000);
			if(res.joininfo.helpnum == 0){
				that.doKan();
			}
		})
	},
	onUnload:function(){
		clearInterval(interval);
	},
	getdjs:function(){
		var that = this
		var totalsec = that.data.product.endtime*1 - that.data.nowtime*1; 
		if(totalsec <=0){
			 var djs = '00时00分00秒';
		}else{
			var date = Math.floor(totalsec/86400);
			var houer = Math.floor((totalsec - date*86400)/3600);
			var min = Math.floor((totalsec - date*86400 - houer *3600)/60);
			var sec = totalsec - date*86400 - houer*3600 - min*60
			var djs = (date>0?date + '天':'') + (houer<10?'0':'')+houer+'时'+(min<10?'0':'')+min+'分'+(sec<10?'0':'')+sec+'秒';
		}
		that.setData({djs:djs})
	},
	doKan:function(){
		var that = this;
		var product = that.data.product;
		$.post('kanjia/kanjiaKan',{joinid:that.data.joininfo.id},function(res){
			if(res.status==0){
				$.alert(res.msg);return;
			}
			var cut_percent = Math.round((product.sell_price*1 - res.joininfo.now_price*1)/(product.sell_price*1-product.min_price*1)*100);
			that.setData({showkanjia:true,helpinfo:res.helpinfo,joininfo:res.joininfo,cut_percent:cut_percent})
			that._onLoad();
		});
	},
	hideKanjiaDialog:function(e){
		this.setData({showkanjia:!this.data.showkanjia})
	},
	shareClick:function(){
		this.setData({sharetypevisible:true})
	},
	handleClickMask:function(){
		this.setData({sharetypevisible:false})
	},
	changeshowdetail:function(){
		this.setData({
			showdetail: !this.data.showdetail
		});
	},
	showPoster:function(){
		var that = this;
		that.setData({showposter:true,sharetypevisible:false});
		$.post('kanjia/getJoinPoster',{proid: that.data.product.id,joinid:that.data.joininfo.id},function(data){
			if(data.status==0){
				$.alert(data.msg);
			}else{
				that.setData({posterpic:data.poster})
			}
		},'努力生成中')
	},
	posterDialogClose:function(){
		this.setData({showposter:false});
	},
	onShareAppMessage: function () {
		var that = this;
		this.setData({sharetypevisible:false});
		var thisurl = 'pages/kanjia/join?proid='+that.data.product.id+'&joinid='+that.data.joininfo.id;
		if (thisurl.indexOf('?') > 0) {
				thisurl += '&scene=' + $.mid
		} else {
				thisurl += '?scene=' + $.mid
		}
		console.log(thisurl);
		return {
			title: '快来帮我砍一刀~ ' + that.data.product.name,
			imageUrl:that.data.product.pics[0],
			path: thisurl
		}
	},
}))